<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
        :disabled="method==='view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="名称" prop="name" :rules="[
                  {required: true, message:'名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort" :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="所在路网" prop="roadReach"
              :rules="[
                {required: true, message:'所在路网不能为空', trigger:'blur'}
               ]">
              <el-checkbox-group
                  v-model="roadReachList">
                 <el-checkbox v-for="item in $dictUtils.getDictList('road_name')" :label="item.value" :key="item.id">{{item.label}}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="标签" prop="tag"
              :rules="[
               ]">
              <el-checkbox-group
                  v-model="tagList">
                 <el-checkbox v-for="item in $dictUtils.getDictList('maker_tag')" :label="item.value" :key="item.id">{{item.label}}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼层" prop="floorId" :rules="[
                {required: true, message:'楼层不能为空', trigger:'blur'}
               ]">
              <SelectTree ref="floor" :props="{
                  value: 'id',             // ID字段名
                  label: 'showName',         // 显示名称
                  children: 'childNodes'    // 子级字段名
                }" url="/smis/floor/jfFloor/treeData" :value="inputForm.floorId" :clearable="true" :accordion="true"
                @getValue="(value) => {inputForm.floorId=value; $refs.selectPoint.showFloor(value);}" />
            </el-form-item>
          </el-col>
          <el-col :span="24" style="height: 400px;">
            <el-form-item label="所在位置" prop="x" :rules="[
               {required: true, message:'请选择所在位置', trigger:'blur'}
              ]">
              <select-point ref="selectPoint" @onMapClick="mapClick"></select-point>
            </el-form-item>
          </el-col>
          <el-col :span="20" v-for="(item, index) in files" :key='index'>
               <el-form-item :label="item.label" :prop="item.value"
                    :rules="[
                     ]">
                  <el-upload-ext
                    :limit = "1"
                    :accept = "item.format"
                    :fileType = "item.fileType"
                    :width = "item.width"
                    :height = "item.height"
                    v-model = "inputForm.file[item.value]"
                  >
                  </el-upload-ext>
               </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="des" :rules="[
                 ]">
              <el-input rows="3" type="textarea" v-model="inputForm.des" placeholder="请填写描述"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  import ElUploadExt from '@/components/elUploadExt'
  export default {
    data() {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        files: [{
          label: '地图logo',
          value: 'map_logo',
          format: '.png',
          fileType: '2',
          width: '128',
          height: '128'
        }, {
          label: 'logo',
          value: 'logo',
          format: '.png',
          fileType: '2',
          width: '128',
          height: '128'
        }],
        inputForm: {
          id: '',
          group: {
            id: ''
          },
          name: '',
          roadReach: '',
          tag: '',
          sort: '',
          des: '',
          file: {
          },
          filesAll: '',
          floorId: '',
          x: '',
          y: '',
          z: '',
          objType: '',
          objId: '',
          ext: ''
        },
        roadReachList: [],
        tagList: []
      }
    },
    components: {
      SelectTree,
      SelectPoint,
      ElUploadExt
    },
    watch: {
      'inputForm.floorId'(val) {
        this.$refs.selectPoint.showFloor(val);
        if (val && this.inputForm.x !== '' && this.inputForm.y !== '') {
          this.$refs.selectPoint.setMarkPoint(val, this.inputForm.x, this.inputForm.y, this.inputForm.z, true);
        }
      }
    },
    methods: {
      init(method, id, searchForm) {
        // console.log(this.$dictUtils.getDictList('road_name'))
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建位置`
        } else if (method === 'edit') {
          this.title = '修改位置'
        } else if (method === 'view') {
          this.title = '查看位置'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.file = {}
          this.roadReachList = []
          this.tagList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/mini/maker/jfMakerList/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({
              data
            }) => {
              this.inputForm = this.recover(this.inputForm, data.jfMakerList)
              if(data.jfMakerList.filesAll && data.jfMakerList.filesAll.length > 0) {
                this.inputForm.file = JSON.parse(data.jfMakerList.filesAll)
              } else {
                this.inputForm.file = {}
              }
              if(this.inputForm.roadReach && this.inputForm.roadReach.length > 1) {
                this.roadReachList = this.inputForm.roadReach.split(",")
              }
              if(this.inputForm.tag && this.inputForm.tag.length > 1) {
                this.tagList = this.inputForm.tag.split(",")
              }
              this.loading = false
              this.$refs.selectPoint.showFloor(this.inputForm.floorId)
              this.$refs.selectPoint.setMarkPoint(this.inputForm.floorId, this.inputForm.x, this.inputForm.y, this.inputForm.z, true)
            })
          } else {
            this.inputForm.group = searchForm.group
          }
        })
      },
      mapClick(e) {
        this.inputForm.x = e.x.toFixed(2)
        this.inputForm.y = e.y.toFixed(2)
        this.inputForm.z = e.z.toFixed(2)
      },
      // 表单提交
      doSubmit() {
        this.inputForm.filesAll = JSON.stringify(this.inputForm.file)
        this.inputForm.roadReach = this.roadReachList.join(',')
        this.inputForm.tag = this.tagList.join(',')
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/mini/maker/jfMakerList/save`,
              method: 'post',
              data: this.inputForm
            }).then(({
              data
            }) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>
